package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Menu> getMenuList(String username) {
        //获取主菜单
        List<Menu> parentMenu=menuDao.getParentMenu(username);
        //遍历主菜单，将二级子菜单加入父集
        for (Menu menu : parentMenu) {
            //获取父菜单的id
            Integer parentMenuId = menu.getId();
            List<Menu> childrenMenu=menuDao.getChildrenMenuByParentMenuId(parentMenuId,username);
            menu.setChildren(childrenMenu);
        }
        return parentMenu;
    }

    @Override
    public List<Integer> getMenuByRoleId(Integer id) {

        return menuDao.getMenuByRoleId(id);
    }
}
